package src.main.java.com.pms.gui.dialog.appointment;

import javax.swing.*;
import java.awt.*;
import src.main.java.com.pms.service.AppointmentService;

public abstract class AppointmentDialog extends JDialog {
    protected JTextField petIdField;
    protected AppointmentService appointmentService;
    protected String appointmentType;

    public AppointmentDialog(JFrame parent, String title, String type) {
        super(parent, title, true);
        this.appointmentType = type;
        appointmentService = new AppointmentService();
        initComponents();
    }

    protected void initComponents() {
        setSize(400, 200);
        setLocationRelativeTo(getParent());

        // 使用现代风格的布局
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 创建输入面板
        JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel idLabel = new JLabel("宠物ID:");
        petIdField = new JTextField(15);
        inputPanel.add(idLabel);
        inputPanel.add(petIdField);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton confirmButton = new JButton("确认预约");
        JButton cancelButton = new JButton("取消");

        // 设置按钮样式
        confirmButton.setPreferredSize(new Dimension(100, 30));
        cancelButton.setPreferredSize(new Dimension(100, 30));

        confirmButton.addActionListener(e -> handleAppointment());
        cancelButton.addActionListener(e -> dispose());

        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);

        // 添加组件到主面板
        mainPanel.add(inputPanel);
        mainPanel.add(Box.createVerticalStrut(20));
        mainPanel.add(buttonPanel);

        add(mainPanel);
    }

    protected void handleAppointment() {
        try {
            int petId = Integer.parseInt(petIdField.getText().trim());
            boolean success = makeAppointment(petId);
            if (success) {
                JOptionPane.showMessageDialog(this, 
                    appointmentType + "预约成功！", 
                    "成功", JOptionPane.INFORMATION_MESSAGE);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, 
                    "预约失败，请检查宠物ID是否正确", 
                    "错误", JOptionPane.ERROR_MESSAGE);
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, 
                "请输入有效的宠物ID", 
                "错误", JOptionPane.ERROR_MESSAGE);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, 
                "预约失败：" + ex.getMessage(), 
                "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    protected abstract boolean makeAppointment(int petId) throws Exception;
} 